package com.zking.ssm.controller;

import com.zking.ssm.model.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Locale;

@Controller
public class HelloController {

    @RequestMapping("/toIndex")
    public String toIndex(){
        return "index"; // /WEB-INF/jsp/index.jsp
    }

    @RequestMapping("/toAdd")
    public String toAdd(){
        return "add";
    }

    @RequestMapping("/hello")
//    @RequestBody(重要~~~~~)：用于目前比较流行的ajax开发的数据绑定(即提交数据的类型为json格式)
    @ResponseBody
    public String hello(String uname){
        System.out.println("hello,world!");
        return "hello"; // /WEB-INF/jsp/hello.jsp
    }

    @RequestMapping("/add")
    public ModelAndView add(User user, ModelAndView mv){
        System.out.println("add");
        System.out.println(user);
        mv.addObject("uname",user.getUname());
        mv.setViewName("add");
        return mv; // /WEB-INF/jsp/add.jsp
    }

    @RequestMapping("/del")
    @ResponseBody
    public String del(){
        System.out.println("del");
        return "del";
    }

    @RequestMapping("/toI18n")
    public String i18n(String locale, HttpSession session){
        if("zh".equals(locale)){
            session.setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME, Locale.CHINA);
        }
        else{
            session.setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME,Locale.US);
        }
        return "index";
    }


}
